using System;
using System.Linq;
using RayDen.Library.Core;
using RayDen.Library.Core.Primitives;

namespace RayDen.RayEngine.Engines.XP
{
    public class WavelengthCluster
    {
        public float LambdaStart, Delta;
        public float[] Values;
        public float[] Lambdas;

        public WavelengthCluster(float start, float d, int n)
        {
            LambdaStart = start;
            Delta = d;
            Values = new float[n];
            Lambdas = Enumerable.Range(0, n).Select(i => start + d*i).ToArray();
        }

        public WavelengthCluster(SpectralDistribution spd, float[] ls)
        {
            LambdaStart = ls[0];
            Values = new float[ls.Length];
            Lambdas = ls.ToArray();
            spd.Sample(ls.Length, ls, Values);
        }

        public WavelengthCluster(float[] ls)
        {
            Values = new float[ls.Length];
            Lambdas = ls.ToArray();
            LambdaStart = ls[0];
        }
        public WavelengthCluster(float[] l, float[] ls)
        {
            Values = ls.ToArray();
            Lambdas = l.ToArray();
            LambdaStart = l[0];
        }


        public WavelengthCluster Add(WavelengthCluster c)
        {
            if ((Math.Abs(c.LambdaStart - LambdaStart) > MathLab.Epsilon)||(c.Values.Length != Values.Length))
            {
                return this;
            }

            for (int i = 0; i < Values.Length; i++)
            {
                Values[i] = Values[i] + c.Values[i];
            }

            return this;
        }


        public WavelengthCluster Mul(WavelengthCluster c)
        {
            if ((Math.Abs(c.LambdaStart - LambdaStart) > MathLab.Epsilon) || (c.Values.Length != Values.Length))
            {
                return this;
            }

            for (int i = 0; i < Values.Length; i++)
            {
                Values[i] = Values[i] * c.Values[i];
            }

            return this;
        }

        public WavelengthCluster Mul(float f)
        {
            for (int i = 0; i < Values.Length; i++)
            {
                Values[i] = Values[i] * f;
            }

            return this;
        }
    }
}